package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.Message;
import com.ruoyi.system.service.IMessageService;

import java.util.List;

import com.ruoyi.system.websocket.MessageForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: CJJ
 * @date: 2024/06/24/10:34
 * @desc:
 */

@RestController
@RequestMapping("/message")
public class MessageController extends BaseController  {

    //逻辑层
    @Autowired
    private IMessageService messageService;

    // 查找未读数量
    @GetMapping("/findNoReadMessageLength")
    public R findNoReadMessage(@RequestParam("userId") String userId) {
        try {
            Integer total = messageService.findNoReadMessageLength(userId);
            return R.ok(total, "查询成功!");
        } catch (Exception e) {
            return R.fail(1, e.getMessage());
        }
    }

    // 查找两个人的聊天记录
    @GetMapping("/findMessageBySendUserAndReceiveUser")
    public R<List<Message>> findMessageBySendUserAndReceiveUser(
            @RequestParam("sendUserId") String sendUserId,
            @RequestParam("receiveUserId") String receiveUserId) {
        try {
            return R.ok(
                    messageService.findMessageBySendUserAndReceiveUser(sendUserId, receiveUserId),
                    "查找成功!");
        } catch (Exception e) {
            return R.fail(1, e.getMessage());
        }
    }
    // 发送信息
    @PostMapping("/sendMessage")
    public R sendMessage(@RequestBody Message message) {
        try {
            messageService.sendMessage(message);
            return R.ok(0, "发送成功!");
        } catch (Exception e) {
            return R.fail(1, e.getMessage());
        }
    }

    // 查找我的用户信息数据
    @GetMapping("/searchUserForForm")
    public R<List<MessageForm>> searchUserForForm(
            @RequestParam("loginUserId") String loginUserId,
            @RequestParam("searchUserName") String searchUserName) {
        try{
            List<MessageForm> messages = messageService.searchUserForForm(
                    loginUserId, searchUserName);
            return R.ok( messages, "查找成功!");
        } catch (Exception e) {
            return R.fail(1, e.getMessage());
        }
    }
}

